using System.Collections.Generic;
using System.Windows.Input;
using Giraffe.Client.Common;
using Giraffe.Core.Common.Messengers;
using Giraffe.Core.Model;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace Giraffe.Client.MainWindowModule.ClientsPanel
{
    public class MessengerListViewModel: ViewModelBase
    {
        private readonly IUser _user;
        private IList<MessengerStatusViewModel> _mesengersStatusTools;
        private DelegateCommand<object> _loginCommand;

        public MessengerListViewModel(IUser user)
        {
            this._user = user;

            RebuildMessengerList();
        }

        public IEnumerable<MessengerStatusViewModel> MessengerStatusTools
        {
            get
            {
                return _mesengersStatusTools;
            }
        }

        private void RebuildMessengerList()
        {
            _mesengersStatusTools = new List<MessengerStatusViewModel>();
            foreach (IMessenger messenger in _user.Messengers)
            {
                _mesengersStatusTools.Add(new MessengerStatusViewModel(messenger));
            }
            RisePropertyChanged("MessengerStatusTools");
        }

        private void OnMessengersListChanged()
        {
            RebuildMessengerList();
        }

        public ICommand LoginCommand
        {
            get
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new DelegateCommand<object>(LoginCommandExecute);
                }
                return _loginCommand;
            }
        }

        private void LoginCommandExecute(object obj)
        {
            _user.Login();
        }
    }
}